<template>
  <PageLayout>
    <template #extra>
      <div class="bg-fff d-flex j-sb card-list p-24 mb-24">
        <div>
          <div>{{ detail.totalNum }}</div>
          <div>当前总商铺</div>
        </div>
        <div>
          <div>{{ detail.addNum }}</div>
          <div>昨日新增商铺</div>
        </div>
        <div>
          <div>{{ detail.abnormalNum }}</div>
          <div>异常商铺</div>
        </div>
      </div>
    </template>
    <template #header>
      <BasicForm :options="queryOptions" @ok="onQueryDataByParams"></BasicForm>
    </template>
    <div class="table-box">
      <div class="head-box">
        <div class="title">门店列表</div>
        <a-space :size="16">
          <a-button @click="edit()">新增门店</a-button>
          <a-button type="primary" @click="exportFile">数据导出</a-button>
        </a-space>
      </div>
      <div class="bg-fff">
        <!-- :apiFetch="apiFetch"  -->
        <BasicTable
          ref="tableRef"
          :columns="columns"
          :query-params="queryData"
          :apiFetch="postShopPage"
        >
          <template #bodyCell="{ column, record }">
            <template v-if="column.key === 'action'">
              <a-space class="space-btn--box" :size="10">
                <a class="btn" @click="toDetail(record)">详情</a>
                <a class="btn" @click="toDdit(record)">编辑</a>
                <a class="btn" @click="toDelete(record)">删除</a>
                <a
                  class="btn"
                  v-if="!record.status"
                  @click="changeStatus(record)"
                  >禁用</a
                >
                <a class="btn" v-else @click="changeStatus(record)">启用</a>
              </a-space>
            </template>
          </template>
        </BasicTable>
      </div>
    </div>
    <EditModal
      ref="editModalRef"
      :data="dataDetail"
      @ok="tableRef.fetch()"
    ></EditModal>
  </PageLayout>
</template>

<script setup>
import EditModal from "./components/edit.vue";
import {
  postShopPage,
  postShopStatistics,
  postShopUpdateStatus,
  postShopExport,
  postShopDelete
} from "@/api/common";
import { useStoreCommonEnum } from "@/store";
import { comfirmModal } from "@/hooks/useModal";
import { message } from "ant-design-vue";
const storeCommonEnum = useStoreCommonEnum();
const router = useRouter();
const queryOptions = ref([
  {
    name: "门店状态",
    type: "select",
    value: undefined,
    placeholder: "请选择",
    filed: "status",
    list: [
      {
        label: "启用",
        value: 0,
      },
      {
        label: "禁用",
        value: 1,
      },
    ],
  },
  {
    name: "店铺名/手机号",
    type: "input",
    value: "",
    placeholder: "请输入",
    filed: "keywords",
    labelCol: { width: "120px" },
  },
  {
    name: "省",
    type: "input",
    value: "",
    placeholder: "请输入",
    filed: "province",
    labelCol: { width: "120px" },
  },
  {
    name: "市",
    type: "input",
    value: "",
    placeholder: "请输入",
    filed: "city",
    labelCol: { width: "120px" },
  },
  {
    name: "区",
    type: "input",
    value: "",
    placeholder: "请输入",
    filed: "area",
    labelCol: { width: "120px" },
  },
  // {
  //   name: "状态",
  //   type: "select",
  //   value: undefined,
  //   placeholder: "请选择",
  //   filed: "status",
  //   list: [
  //     {
  //       label: "正常",
  //       value: "1",
  //     },
  //     {
  //       label: "欠费",
  //       value: "2",
  //     },
  //   ],
  // },
]);
const detail = ref({});
const columns = [
  {
    title: "序号",
    dataIndex: "index",
    width: "80px",
    customRender: (record) => `${record.index + 1}`,
  },
  {
    title: "商铺ID",
    dataIndex: "shopNo",
    key: "shopNo",
    width: "150px",
    ellipsis: true,
  },
  {
    title: "门店名称",
    dataIndex: "name",
    key: "name",
    width: "150px",
    ellipsis: true,
  },
  {
    title: "总信用分",
    dataIndex: "totalAmount",
    key: "totalAmount",
    width: "150px",
    ellipsis: true,
  },
  {
    title: "剩余信用分",
    dataIndex: "balance",
    key: "balance",
    width: "150px",
    ellipsis: true,
  },
  {
    title: "地区",
    dataIndex: "aa",
    key: "aa",
    width: "150px",
    ellipsis: true,
    customRender: ({ record }) => {
      return `${record.province}/${record.city}/${record.area}`;
    },
  },
  {
    title: "设备数",
    dataIndex: "deviceNum",
    key: "deviceNum",
    width: "150px",
    ellipsis: true,
  },
  {
    title: "老板姓名",
    dataIndex: "shopUserName",
    key: "shopUserName",
    width: "150px",
    ellipsis: true,
  },
  {
    title: "手机号",
    dataIndex: "shopUserPhone",
    key: "shopUserPhone",
    width: "150px",
    ellipsis: true,
  },
  // {
  //   title: "区域经理",
  //   dataIndex: "aa",
  //   key: "aa",
  //   width: "150px",
  //   ellipsis: true,
  // },
  // {
  //   title: "代理商",
  //   dataIndex: "agentName",
  //   key: "agentName",
  //   width: "150px",
  //   ellipsis: true,
  // },
  // {
  //   title: "客户经理",
  //   dataIndex: "aa",
  //   key: "aa",
  //   width: "150px",
  //   ellipsis: true,
  // },
  {
    title: "门店状态",
    dataIndex: "status",
    key: "status",
    width: "150px",
    ellipsis: true,
    customRender: ({ record }) => `${record.status ? "停用" : "启用"}`,
  },
  {
    title: "建档日期",
    dataIndex: "createTime",
    key: "createTime",
    width: "150px",
    ellipsis: true,
  },

  {
    title: "操作",
    fixed: "right",
    dataIndex: "action",
    key: "action",
    width: 230,
  },
];
onMounted(() => {
  postShopStatistics().then((res) => {
    detail.value = res.data;
  });
});

const queryData = reactive({});
const tableRef = ref(null);
const onQueryDataByParams = (params) => {
  for (let key in params) {
    queryData[key] = params[key];
  }
  tableRef.value.refreshTableData();
};

const edit = (record) => {
  router.push({
    path: "/store/manage/edit",
    query: {
      id: record?.id,
    },
  });
};
const toDetail = (record) => {
  router.push({
    path: "/store/manage/detail",
    query: {
      id: record?.id,
    },
  });
};

const changeStatus = (record) => {
  // const title = record.status == 0 ? "禁用" : "启用";
  const title = `${record.status ? "启用" : "禁用"}`;
  comfirmModal({
    content: `确认${title}`,
    onOk() {
      postShopUpdateStatus({
        id: record.id,
        status: !record.status,
      }).then(() => {
        message.success(`${title}成功`);
        tableRef.value.fetch();
      });
    },
  });
};
const exportFile = () => {
    if (!tableRef.value.data?.length) {
    message.error("暂无数据");
    return;
  }
  const params = {};

  for (let key in queryData) {
    params[key] = queryData[key];
  }
  postShopExport(params).then((res) => {
    message.success("导出成功");
  });
};

const type = ref("");
const dataDetail = ref({});
const editModalRef = ref(null);
const toDdit = (record, val) => {
  dataDetail.value = record;
  setTimeout(() => {
    editModalRef.value.visible = true;
  }, 0);
};
const toDelete = (record, val) => {
  comfirmModal({
    content: `确认删除该门店`,
    onOk() {
      postShopDelete({
        id: record.id,
        // shopUserId: record.id,
      }).then(() => {
        message.success(`删除成功`);
        tableRef.value.fetch();
      });
    },
  });
};

const dataSource = ref(
  Array.from({ length: 11 }, () => ({
    spaceName: "2121",
  }))
);
</script>

<style lang="less" scoped>
.card-list {
  margin-bottom: 24px;
  display: flex;
  gap: 50px;
  > div {
    // flex: 1;
    width: 300px;
    padding: 10px;
    // height: 100px;
    border-radius: 8px;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    color: #fff;
    font-size: 30px;
    > div:first-child {
      font-size: 48px;
      font-weight: 600;
    }
    &:nth-child(1) {
      background: #02a7f0;
    }
    &:nth-child(2) {
      background: #66cc99;
    }
    &:nth-child(3) {
      background: #e62129;
    }
  }
}
</style>
